<template>
	<view>
		<nav-bar>合同签约</nav-bar>
		<view class="page-box">
			<u-alert-tips type="warning" :title="title" :description="description" :show-icon="true" :v-if="tipDisable"></u-alert-tips>
			<u-toast ref="uToast" />
			<view class="order">
				<view class="top">
					<view class="left">
						<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
						<view class="store">企业签署</view>
						<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
					</view>
					<view class="right">{{enterPriseList.uploaded==true? '已签约':'未签约'}}</view>
				</view>
				<view class="item">
					<!-- 展示 -->
					<view class="left" v-if="!enterPriseVi" v-for="(item,index) in enterPriseList.imageList" :key="item.id">
						<image width="100%" height="300rpx" :src="item.src" mode="aspectFit"></image>
					</view>
					<!-- 上传 -->
					<view class="left" v-if="enterPriseVi">
						<u-upload @on-choose-fail="onChooseFail" :before-remove="beforeRemove" ref="uUpload"
							:custom-btn="customBtn" :show-upload-list="showUploadList" :action="action"
							:auto-upload="autoUpload" :file-list="fileList" :show-progress="showProgress"
							:deletable="deletable" :max-count="maxCount" :form-data="formData" @on-list-change="onListChange"
							@on-success="onSuccess">
							<view v-if="customBtn" slot="addBtn" class="slot-btn" hover-class="slot-btn__hover"
								hover-stay-time="150">
								<u-icon name="photo" size="60" :color="$u.color['lightColor']"></u-icon>
							</view>
						</u-upload>
					</view>

				</view>

				<view class="bottom">
					<view class="more">
						<u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
					</view>
					<view class="content">
						<view class="evaluate delivery-time" v-if="enterPriseList.uploaded">处理时间 {{enterPriseList.upload_time}}</view>
					</view>

					<view class="evaluate btn" v-if="enterPriseVi" @click="upload()">签约</view>
				</view>
			</view>
			<u-line color="grey" />
			<view class="order">
				<view class="top">
					<view class="left">
						<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
						<view class="store">个人签署</view>
						<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
					</view>
					<view class="right">{{userList.uploaded==true? '已签约':'未签约'}}</view>
				</view>
				<view class="item">
					<!-- 展示 -->
					<view class="left" v-if="!userVi" v-for="(item,index) in userList.imageList" :key="item.id">
						<image width="100%" height="300rpx" :src="item.src" mode="aspectFit"></image>
					</view>
					<!-- 上传 -->
					<view class="left" v-if="userVi">
						<u-upload @on-choose-fail="onChooseFail" :before-remove="beforeRemove" ref="uUpload"
							:custom-btn="customBtn" :show-upload-list="showUploadList" :action="action"
							:auto-upload="autoUpload" :file-list="fileList" :show-progress="showProgress"
							:deletable="deletable" :max-count="maxCount" :form-data="formData" @on-list-change="onListChange"
							@on-success="onSuccess">
							<view v-if="customBtn" slot="addBtn" class="slot-btn" hover-class="slot-btn__hover"
								hover-stay-time="150">
								<u-icon name="photo" size="60" :color="$u.color['lightColor']"></u-icon>
							</view>
						</u-upload>
					</view>

				</view>

				<view class="bottom">
					<view class="more">
						<u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
					</view>
					<view class="content">

						<view class="evaluate delivery-time" v-if="userList.uploaded">处理时间 {{userList.upload_time}}</view>
					</view>

					<view class="evaluate btn" v-if="userVi" @click="upload()">签约</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				action: 'http://localhost:9090/',
				// 预置上传列表
				fileList: [],
				// fileList: [{
				// 	url: 'http://pics.sc.chinaz.com/files/pic/pic9/201912/hpic1886.jpg',
				// 	error: false,
				// 	progress: 100
				// }],
				formData:{
					value:''
				},
				showUploadList: true,
				customBtn: false,
				autoUpload: false,
				showProgress: true,
				deletable: true,
				customStyle: false,
				maxCount: 6,
				lists: [], // 组件内部的文件列表
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore'],
				UserID: '',
				userVi: false,
				enterPriseVi: false,
				userList: {
					id: '',
					imageList: [{
							src: '../../../static/images/user/avatar.jpg'
						},
						{
							src: '../../../static/images/user/avatar.jpg'
						},
						{
							src: '../../../static/images/user/avatar.jpg'
						}
					],
					uploaded:'',
					upload_time:'',
				},
				enterPriseList: {
					id: '',
					imageList: [{
							src: '../../../static/images/user/avatar.jpg'
						},
						{
							src: '../../../static/images/user/avatar.jpg'
						},
						{
							src: '../../../static/images/user/avatar.jpg'
						}
					],
					uploaded:'',
					upload_time:'',
				},
				title: '签署限制',
				description: '只能签署一次，请全部选择后统一上传！',
				tipDisable:false
			}
		},
		onLoad: function(option) {
			console.log(option.typeid)
			let userAction='http://localhost:9090/function/userSignContract/'+option.id
			let enterPriseAction='http://localhost:9090/function/enterPriseSignContract/'+option.id
			if (option.typeid == 'user') {
				this.action=userAction
			} else if (option.typeid == 'enterPrise') {
				this.action=enterPriseAction
			}
			this.UserID=option.id
			this.lookForContractInfo(option.typeid,option.id,option.index)
		},
		methods: {
			lookForContractInfo(type,id,index){
			    this.$api.functionList.lookForContract(type,id,index).then(res=>{
					console.log(res)
					if(res.data.code==200){
						let data=res.data.data
						this.formData.value=data.id
						console.log("user:"+data.user_upload)
						if(type=="user"){
							if(data.user_upload){
								this.userVi=false
								this.showTip('user')
							}else{
								this.userVi=true
							}
						}
						else if(type=="enterPrise"){
							if(data.enter_prise_upload){
								this.enterPriseVi=false
								this.showTip('enterPrise')
							}else{
								this.enterPriseVi=true
							}
						}
						console.log(data)
						data.user_upload_time=this.getResult('yyyy-mm-dd hh:MM',data.user_upload_time);//日期格式化
						data.enter_prise_upload_time=this.getResult('yyyy-mm-dd hh:MM',data.enter_prise_upload_time);//日期格式化
						let imgList=[[],[]]
						let imgData=[data.enter_prise_voucher,data.user_voucher]
						console.log(imgData)
						imgData.forEach((item,index)=>{
							
							if(item!=""){
								if(index==0){
									let imgEnter=item.split(',')
									imgEnter.forEach((item2,index2)=>{
										if(item2!=""){
											let value={src:item2}
											  let modify=JSON.parse(JSON.stringify(value))
											// console.log(modify)
											imgList[1].push(modify)
										}else{
											return false
										}
										
									})
								}
								else if(index==1){
									let imgUser=item.split(',')
									imgUser.forEach((item2,index2)=>{
										if(item2!=""){
										let value={src:item2}
										  let modify=JSON.parse(JSON.stringify(value))
										// console.log(modify)
										imgList[0].push(modify)
										}else{
											return false
										}
									})
								}
								
							}
							else{
									let value={src:'../../../static/empty/data.png'}
									let modify=JSON.parse(JSON.stringify(value))
									index==0? imgList[1].push(modify):imgList[0].push(modify)
							}
						})
						console.log(imgList)
					    this.userList={
							id:data.user_id,
							uploaded:data.user_upload,
							upload_time:data.user_upload_time,
							imageList:imgList[0]
						}
						this.enterPriseList={
							id:data.enter_prise_id,
							uploaded:data.enter_prise_upload,
							upload_time:data.enter_prise_upload_time,
							imageList:imgList[1]
						}
						console.log(this.userList)
						console.log(this.enterPriseList)
						
					}
					
				}).catch(err=>{
					
				})
			},
			getResult(format,data) {
				data = this.$u.timeFormat(data, format);
				return data;
			},
			showTip(Tpye){
				Tpye=='user'? Tpye='企业':Tpye='用户'
				this.title="签署提示"
				this.description="请等待"+Tpye+"进行签署,再进行下一步操作。"
			},
			reUpload() {
				this.$refs.uUpload.reUpload();
			},
			clear() {
				this.$refs.uUpload.clear();
			},
			upload() {
				this.$refs.uUpload.upload();
			},
			deleteItem(index) {
				this.$refs.uUpload.remove(index);
			},
			onOversize(file, lists) {
				// console.log('onOversize', file, lists);
			},
			onPreview(url, lists) {
				// console.log('onPreview', url, lists);
			},
			onRemove(index, lists) {
				// console.log('onRemove', index, lists);
			},
			onSuccess(data, index, lists) {
				 this.showToast()
				 setTimeout(()=>{
					 this.userVi=false
					 this.enterPriseVi=false
				 },1200)
			},
			onChange(res, index, lists) {
				// console.log('onChange', res, index, lists);
			},
			error(res, index, lists) {
				// console.log('onError', res, index, lists);
			},
			onProgress(res, index, lists) {
				// console.log('onProgress', res, index, lists);
			},
			onUploaded(lists) {
				// console.log('onUploaded', lists);
			},
			onListChange(lists) {
				// console.log('onListChange', lists);
				this.lists = lists;
				
			},
			beforeRemove(index, lists) {
				return true;
			},
			onChooseFail(e) {
				// console.log(e);
			},
			Query(){
				let id=this.formData.value
				this.$api.functionList.lookForContract("both",id).then(res=>{
					console.log(res)
					if(res.data.data=="True"){
						console.log("true")
						this.showMove()
						setTimeout(()=>{
							uni.switchTab({
							    url: '/pages/tabbar/my/user'
							});
						},1200)
					}
				})
			},
			showToast() {
				this.Query()
							this.$refs.uToast.show({
								title: '上传成功',
								type: 'success',
								icon:true,
							
							})
						},
			showMove() {
							this.$refs.uToast.show({
								title: '签约合同成功',
								type: 'success',
								icon:true,
							})
						}

		}
	}
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss">
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 40rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>
